<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				initValue:{
					posId: "",
					instId: "",
					fileList:[]//上次保存的文件列表
				},
				payapplicationformCacheKey:'fin-bx-expenses-data-info-payapplicationform-cache',
				userCacheKey:'fin-bx-expenses-data-info-user-cache',
				instCacheKey:'fin-bx-expenses-data-info-inst-cache',
				bxUserId:'',
			})
		},
		watch:{
			'formdata.userId':{
				handler(newValue,oldValue){
					 let _that = this.$refs.autoform
					 if(_that && newValue){
						 if(this.bxUserId != newValue){
							 this.om.toast("付款经办人与报销人不同，请选择付款申请条目号", 5000)
							_that.changeEditor("title2","payApplicationFormDataShowId","required",true)
						 }else{
							 _that.changeEditor("title2","payApplicationFormDataShowId","required",false)
						 }
					 }
					
				}
			},
		},
		methods: {
			onSelectUser(userId){
				
			},
			onInputPlanPayNum(newValue){
				var diff = minusF(ptFloat(newValue),ptFloat(this.formdata.paidNum))
				if(diff < 0){
					this.$nextTick(function(){
						this.$refs.autoform.formdata.planPayNum = this.formdata.paidNum
					})
					this.om.toast("票据金额不可小于已付款金额")
					return
				}
				if(this.formdata.applyPayNum){
					var diff2 = minusF(ptFloat(diff),ptFloat(this.formdata.applyPayNum))
					if(diff2 < 0){
						this.$nextTick(function(){
							this.$refs.autoform.formdata.planPayNum = this.formdata.paidNum
						})
						this.om.toast("票据金额减去已付款金额应不小于申请金额")
					}
				}
			},
			onInputPaidNum(newValue){
				if(newValue){
					if(hasValue(this.formdata.planPayNum) && this.formdata.planPayNum !== ""){
						var diff = minusF(ptFloat(this.formdata.planPayNum),ptFloat(newValue))
						if(diff < 0){
							this.$nextTick(function(){
								this.$refs.autoform.formdata.paidNum = 0
							})
							this.om.toast("已付款金额不能大于票据金额")
							return
						}
						if(this.formdata.applyPayNum){
							var diff2 = minusF(ptFloat(diff),ptFloat(this.formdata.applyPayNum))
							if(diff2 < 0){
								this.$nextTick(function(){
									this.$refs.autoform.formdata.paidNum = 0
								})
								this.om.toast("票据金额减去已付款金额应不小于申请金额")
								return 
							}
						}
					}
				}
			},
			onInputApplyPayNum(newValue){
				if(newValue){
					if(!this.formdata.planPayNum){
						this.$nextTick(function(){
							this.$refs.autoform.formdata.applyPayNum = ''
						})
						this.om.toast("请先填写票据金额")
						return
					}
					var diff = minusF(ptFloat(this.formdata.planPayNum),ptFloat(this.formdata.paidNum))
					if(this.formdata.applyPayNum){
						var diff2 = minusF(ptFloat(diff),ptFloat(newValue))
						if(diff2 < 0){
							this.$nextTick(function(){
								this.$refs.autoform.formdata.applyPayNum = ''
							})
							this.om.toast("申请金额不能大于票据金额减去已付款金额")
							return 
						}
					}
				}
				if(!this.checkedit){
					this.$refs.autoform.formdata.selfPayNum = this.formdata.applyPayNum
				}
			},
			onInputSelfPayNum(newValue){
				var diff = minusF(ptFloat(this.formdata.applyPayNum),ptFloat(this.formdata.selfPayNum))
				if(diff < 0){
					this.$nextTick(function(){
						this.$refs.autoform.formdata.selfPayNum = 0
					})
					this.om.toast("核准报销金额不能大于申请报销金额")
				}
			},
			onInputInvoiceNumber(item){
				this.$refs.autoform.changeEditor("title2", "payApplicationFormDataShowId", "linkParam", {
					other:JSON.stringify({
						firstId: '',
						filterIdList:'',
						userId:_that.bxUserId
					}),
					namefield:"onlyCode",
					notefield:"joinRemark",
					extrafield:"",
					searchfield:"q",
					initload:true,
					url:"payApplicationFormShow/findUnReimbursementPage",
					cachename:_that.payapplicationformCacheKey,
					placeholder:"请输入关键字查询"
				})
			},
			verifyPayApplicationForm(){
				return true
			},
			clearPayApplicationForm(){
				let _that = this.$refs.autoform
				_that.clear("title2","payApplicationFormDataShowId")
				_that.formdata.posId = '';
				_that.formdata.posName = '';
				_that.formdata.instId = '';
				_that.formdata.instName = '';
				_that.formdata.remark = '';
				_that.formdata.paidNum = '';
				_that.formdata.payeeName = '';
				_that.formdata.planPayNum = '';
				_that.formdata.selfPayNum = '';
				_that.formdata.invoiceNumber = '';
				_that.formdata.taxRate = '';
				_that.formdata.itemCode = '';
				_that.formdata.itemName = '';
				_that.formdata.itemType = '';
				_that.formdata.accountId = '';
				_that.formdata.accountName = '';
				_that.formdata.accountCode = '';
				_that.formdata.accountParentId = '';
				_that.formdata.accountParentName = '';
			},
			onSelectAccount(account){
				this.formdata.accountCode = account.code;
				if(account.layerNum == 1) {
					this.$refs.autoform.formdata.accountParentId = account.id;
					this.$refs.autoform.formdata.accountParentName = account.dicname;
				}else{
					let _that = this
					this.om.request("accountDict/findLevel1Account",{
						accountDictId: account.id
					}).then(res=>{
						if(res.succeed){
							var value = res.value
							_that.$refs.autoform.formdata.accountParentId = value.id;
							_that.$refs.autoform.formdata.accountParentName = value.dicname;
						}else{
							_that.om.toast(res.msg)
						}
					})
				}
			},
			clearAccount(){
				var formdata = this.$refs.autoform.formdata
				this.$refs.autoform.clear("title2", "accountId")				
				formdata.accountCode = ""
				formdata.accountParentId = ""
				formdata.accountParentName = ""
			},
			initLoad(){
				let _that = this
				//会计科目
				_that.om.request('accountDict/getCombobox', {
					selectInBudget: true					
				}).then(res => {
					_that.$refs.autoform.changeEditor("title2","accountId","data",configSelectData(res,"id","dicname"))
					_that.$refs.autoform.initSelectIndex("title2", "accountId")
				});
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.bxUserId = e.bxUserId
				_that.initValue.userId = e.userId
				_that.initValue.userName = e.userName
				_that.initValue.posId = e.posId
				_that.initValue.posName = e.posName
				_that.initValue.instId = e.instId
				_that.initValue.instName = e.instName
				_that.initValue.partakeInstId = e.instId
				_that.initValue.partakeInstName = e.instName
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor(_that.autoformdata[0].editors,"invoiceDate", "max", getDateStr(new Date()))
								changeEditor(_that.autoformdata[0].editors, "userId","link","../../public/pageSearch/pageSearch")
								changeEditor(_that.autoformdata[0].editors, "userId", "linkParam", {
									other:JSON.stringify({
										firstUserId:e.bxUserId
									}),
									namefield:"userName",
									notefield:"posName",
									extrafield:"instShortName",
									searchfield:"q",
									initload:true,
									url:"orgInstPosUserOfficial/findPagerByOrgId",
									cachename:_that.userCacheKey,
									placeholder:"请输入姓名或岗位"
								})
								changeEditor(_that.autoformdata[0].editors, "partakeInstId","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "partakeInstId", "linkParam", {
									method:"post",
									namefield:"instname",
									extrafield:"joinRemark",
									searchfield:"instName",
									initload:true,
									url:"tinstitution/findNotBoard1",
									cachename:_that.instCacheKey,
									placeholder:"请输入机构名称"
								})
								changeEditor(_that.autoformdata[0].editors, "payApplicationFormDataShowId","errorMsg","")
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "7")
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
								changeEditor(_that.autoformdata[0].editors, "payApplicationFormDataShowId","link","../../public/pageSearch/pageSearch")
								changeEditor(_that.autoformdata[0].editors, "payApplicationFormDataShowId", "linkParam", {
									other:JSON.stringify({
										firstId: '',
										filterIdList:'', 
										userId:_that.bxUserId
									}),
									namefield:"onlyCode",
									notefield:"joinRemark",
									searchfield:"q",
									initload:true,
									url:"payApplicationFormShow/findUnReimbursementPage",
									cachename:_that.payapplicationformCacheKey,
									placeholder:"请输入关键字查询"
								})
								_that.initLoad()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.userCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.userId = d.userId
							formdata.userName = d.userName
							formdata.posId = d.posId
							formdata.posName = d.posName
							formdata.instId = d.instId
							formdata.instName = d.instName
							formdata.partakeInstId = d.instId
							formdata.partakeInstName = d.instName
							_that.onSelectUser(d.userId)
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.instCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.partakeInstId = d.id
							formdata.partakeInstName = d.instname
							uni.removeStorage({
								key: _that.instCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.payapplicationformCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.payApplicationFormDataShowId = d.id;
							formdata.payApplicationFormCode = d.code;
							formdata.posId = d.posId;
							formdata.posName = d.posName;
							formdata.instId = d.instId;
							formdata.instName = d.instName;
							formdata.remark = d.joinRemark;
							formdata.paidNum = d.payNum;
							formdata.payeeName = d.payeeName;
							formdata.planPayNum = "";
							formdata.selfPayNum = "";
							formdata.invoiceNumber = d.invoiceNumber;
							formdata.taxRate = d.taxRate;
							formdata.itemCode = d.itemCode;
							formdata.itemName = d.itemName;
							formdata.itemType = d.itemType;
							formdata.accountId = d.accountId;
							formdata.accountName = d.accountName;
							formdata.accountCode = d.accountCode;
							formdata.accountParentId = d.accountParentId;
							formdata.accountParentName = d.accountParentName;
							uni.removeStorage({
								key: _that.payapplicationformCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
